home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MSG Demo 1.4.source Folder / Demo ƒ / Shell ƒ / dialogs.c < prev    next >
Text File  |  1994-04-15  |  5KB  |  119 lines

  1. /**********************************************************************\
  2.  
  3. File:        dialogs.c
  4.  
  5. Purpose:    This module handles positioning a dialog on the screen as
  6.             per Human Interface Guidelines.  Also, a quick-and-dirty
  7.             default button outline, and a proc filter for dialogs to
  8.             map RETURN or ENTER to button 1 and ESCAPE or COMMAND-PERIOD
  9.             to button 2.
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "dialogs.h"
  29.  
  30. void PositionDialog(ResType theType, short theID)
  31. /* call this function BEFORE loading an alert or dialog box; this function will
  32.    load the alert/dialog into memory and position it centered horizontally and
  33.    positioned 1:3 vertically.  Then, load the alert/dialog right after the call
  34.    to PositionDialog, and the alert/dialog will be loaded from the copy in
  35.    memory, which has already been positioned correctly.  Cute, eh?  See error.c
  36.    for an example. */
  37. {
  38.     Handle                theTemplate;    /* Handle to resource template    */
  39.     register Rect        *theRect;        /* Bounding box of dialog        */
  40.     register short        left;            /* Left side of centered rect    */
  41.     register short        top;            /* Top side of centered rect    */
  42.     
  43.     /* The first field of the resource template for DLOG's and ALRT's */
  44.     /* is its bounding box.  Get a pointer to this rectangle.  This   */
  45.     /* handle dereferencing is safe since the remaining statements in */
  46.     /* this function do not move memory (assignment and simple math). */
  47.  
  48.     theTemplate = GetResource(theType, theID);
  49.     if (theTemplate == 0)
  50.         return;
  51.     theRect=(Rect*)*theTemplate;    /* bounding rectangle */
  52.     
  53.     left = (screenBits.bounds.right - (theRect->right - theRect->left)) / 2;
  54.     top = (screenBits.bounds.bottom - (theRect->bottom - theRect->top)) / 3;
  55.     if (top < (GetMBarHeight() + 1))    /* don't put it over menu bar */
  56.         top = GetMBarHeight() + 1;
  57.  
  58.     theRect->right += left - theRect->left;
  59.     theRect->left = left;
  60.     theRect->bottom += top - theRect->top;
  61.     theRect->top = top;
  62. }
  63.  
  64. pascal void OutlineDefaultButton(DialogPtr myDlog, short itemNum)
  65. /* Use this as the useritem for a dialog which needs an outlined default button. */
  66. /* Make sure the default button is item 1 in the DITL. */
  67. {
  68.     short            itemType;
  69.     Handle            itemH;
  70.     Rect            box;
  71.     
  72.     GetDItem(myDlog, 1, &itemType, &itemH, &box);
  73.     PenSize(3, 3);
  74.     InsetRect(&box, -4, -4);
  75.     FrameRoundRect(&box, 16, 16);
  76.     PenNormal();
  77. }
  78.  
  79. pascal Boolean ProcOFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  80. /* use this as the proc filter when calling ModalDialog -- it maps RETURN and
  81.    ENTER to button 1, and ESCAPE and COMMAND-PERIOD to button 2.  Of course,
  82.    button 1 should be the OK button and button 2 should be the cancel button. */
  83. {
  84.     unsigned char    theChar;
  85.     short            itemType;
  86.     Handle            itemH;
  87.     Rect            box;
  88.     unsigned long    dummy;
  89.     
  90.     switch (theEvent->what)    /* examine event record */
  91.     {
  92.         case keyDown:    /* keypress */
  93.         case autoKey:
  94.             theChar=theEvent->message & charCodeMask;    /* get ascii char value */
  95.             if ((theChar==0x0d) || (theChar==0x03))        /* RETURN or ENTER */
  96.             {
  97.                 *theItem=1;        /* as if the user selected item #1 */
  98.                 GetDItem(theDialog, 1, &itemType, &itemH, &box);
  99.                 HiliteControl((ControlHandle)itemH, 1);    /* flash button 1 by highlighting, */
  100.                 Delay(8, &dummy);                        /* waiting 8 ticks, and then */
  101.                 HiliteControl((ControlHandle)itemH, 0);    /* unhighlighting -- believe */
  102.                 return TRUE;                            /* it or not, that's Apple's */
  103.             }                                            /* preferred method */
  104.             if ((theChar==0x1b) ||    /* ESCAPE */
  105.                 ((theEvent->modifiers & cmdKey) && (theChar=='.')))    /* COMMAND-PERIOD */
  106.             {
  107.                 *theItem=2;        /* as if the user selected item #2 */
  108.                 GetDItem(theDialog, 2, &itemType, &itemH, &box);    /* same as above */
  109.                 HiliteControl((ControlHandle)itemH, 1);
  110.                 Delay(8, &dummy);
  111.                 HiliteControl((ControlHandle)itemH, 0);
  112.                 return TRUE;
  113.             }
  114.             break;
  115.     }
  116.     
  117.     return FALSE;    /* no faking, proceed as planned */
  118. }
  119.